-include Rules.make

MAKE_JOBS ?= 1

all: linux  u-boot-spl  linux-dtbs  
clean: linux_clean  u-boot-spl_clean  linux-dtbs_clean  
install: linux_install  u-boot-spl_install  linux-dtbs_install 
# Kernel build targets
linux: 
	@echo =================================
	@echo     Building the Linux Kernel
	@echo =================================
#	$(MAKE) -C $(LINUXKERNEL_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) O=$(KERNEL_KBUILD_OUTPUT_DIR) $(DEFCONFIG)
	$(MAKE) -j $(MAKE_JOBS) -C $(LINUXKERNEL_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) O=$(KERNEL_KBUILD_OUTPUT_DIR) LOADADDR=$(LOADADDR) uImage
	$(MAKE) -j $(MAKE_JOBS) -C $(LINUXKERNEL_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) O=$(KERNEL_KBUILD_OUTPUT_DIR) modules

linux_install: linux-dtbs_install
	@echo ===================================
	@echo     Installing the Linux Kernel
	@echo ===================================
	@if [ ! -d $(DESTDIR) ] ; then \
		echo "The extracted target filesystem directory doesn't exist."; \
		echo "Please run setup.sh in the SDK's root directory and then try again."; \
		exit 1; \
	fi
	install -d $(DESTDIR)/boot
	install $(KERNEL_KBUILD_OUTPUT_DIR)/arch/arm/boot/zImage $(DESTDIR)/boot
	install $(KERNEL_KBUILD_OUTPUT_DIR)/vmlinux $(DESTDIR)/boot
	install $(KERNEL_KBUILD_OUTPUT_DIR)/System.map $(DESTDIR)/boot
	$(MAKE) -C $(KERNEL_KBUILD_OUTPUT_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) O=$(KERNEL_KBUILD_OUTPUT_DIR) INSTALL_MOD_PATH=$(DESTDIR) INSTALL_MOD_STRIP=$(INSTALL_MOD_STRIP) modules_install

linux_clean:
	@echo =================================
	@echo     Cleaning the Linux Kernel
	@echo =================================
	$(MAKE) -C $(LINUXKERNEL_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) mrproper

# u-boot build targets
u-boot-spl: u-boot
u-boot-spl_clean: u-boot_clean
u-boot-spl_install: u-boot_install

u-boot:  u-boot_clean
	@echo ===================================
	@echo    Building U-boot
	@echo ===================================
#	$(MAKE) -j $(MAKE_JOBS) -C $(LINUXUBOOT_DIR) CROSS_COMPILE=$(CROSS_COMPILE) $(UBOOT_MACHINE) O=$(UBOOT_KBUILD_OUTPUT_DIR)
	$(MAKE) -j $(MAKE_JOBS) -C $(LINUXUBOOT_DIR) CROSS_COMPILE=$(CROSS_COMPILE) DTC=$(KERNEL_KBUILD_OUTPUT_DIR)/scripts/dtc/dtc O=$(UBOOT_KBUILD_OUTPUT_DIR)

u-boot_config: 
	@echo ===================================
	@echo    Building U-boot config  add by Richard
	@echo "config file:"$(UBOOT_MACHINE)
	@echo ===================================
	$(MAKE) -j $(MAKE_JOBS) -C $(LINUXUBOOT_DIR) CROSS_COMPILE=$(CROSS_COMPILE) $(UBOOT_MACHINE) O=$(UBOOT_KBUILD_OUTPUT_DIR)

u-boot_clean:
	@echo ===================================
	@echo    Cleaining U-boot
	@echo ===================================
	$(MAKE) -C $(LINUXUBOOT_DIR)  CROSS_COMPILE=$(CROSS_COMPILE) distclean

u-boot_install:
	@echo ===================================
	@echo    Installing U-boot
	@echo ===================================
	@echo "Nothing to do"


# 
# Kernel DTB build targets
linux-dtbs:
	@echo =====================================
	@echo     Building the Linux Kernel DTBs
	@echo =====================================
#	$(MAKE) -C $(LINUXKERNEL_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) O=$(KERNEL_KBUILD_OUTPUT_DIR) $(DEFCONFIG)
	$(MAKE) -j $(MAKE_JOBS) -C $(LINUXKERNEL_DIR) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) O=$(KERNEL_KBUILD_OUTPUT_DIR) am335x-evmsk.dtb

linux-dtbs_install:
	@echo =======================================
	@echo     Installing the Linux Kernel DTBs
	@echo =======================================
	@if [ ! -d $(DESTDIR) ] ; then \
		echo "The extracted target filesystem directory doesn't exist."; \
		echo "Please run setup.sh in the SDK's root directory and then try again."; \
		exit 1; \
	fi
	install -d $(DESTDIR)/boot
	@cp -f $(KERNEL_KBUILD_OUTPUT_DIR)/arch/arm/boot/dts/*.dtb $(DESTDIR)/boot/

linux-dtbs_clean:
	@echo =======================================
	@echo     Cleaning the Linux Kernel DTBs
	@echo =======================================
	@echo "Nothing to do"


